<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {
	
	public function __construct() {
		parent::__construct();
	}
	
	/**
	 * 
	 * 获取Smarty对象
	 */
	public function getViewEngine() {
		return $this->smartyview->getSmarty();
	}
	
	/**
	 * 
	 * 分配变量至模板
	 * @param string $key
	 * @param string $value
	 * @param bool $noCache
	 */
	public function assign($key, $value, $noCache = false) {
		$this->smartyview->assign($key, $value, $noCache);
	}
	
	/**
	 * 
	 * 显示指定模板
	 * @param string $telName
	 */
	public function display($telName) {
		$this->smartyview->display($telName);
		exit();
	}
	
	/**
	 * 
	 * 是否为post请求
	 */
	public function isPost() {
		return $_SERVER["REQUEST_METHOD"] == "POST";
	}
	
	/**
	 * 
	 * 是否为ajax请求
	 */
	public function isAjax() {
		return $this->input->is_ajax_request();
	}
	
	/**
	 * 
	 * 输出json字符串
	 * @param array $array
	 */
	public function outputJson($array) {
		$jsonStr = $this->output
			->set_content_type('application/json')
			->set_output(json_encode($array))
			->get_output();
		
		$this->outputStr($jsonStr);
	}
	
	/**
	 * 
	 * 输出字符串
	 * @param string $str
	 */
	public function outputStr($str) {
		echo $str;
		exit();
	}
	
	/**
	 * 
	 * 获取输入参数
	 * @param string $key
	 * @param mixed $def
	 */
	public function getParameter($key, $def = null) {
		$param = $this->input->get_post($key, true);
		if ($param === false) {
			return $def;
		}
		else {
			return $param;
		}
	}
	
	/**
	 * 
	 * 获取session数据
	 * @param srting $key
	 * @param mixed $def
	 */
	public function getUserSession($key, $def = null) {
		$data = $this->session->userdata($key);
		if ($data === false) {
			return $def;
		}
		else {
			return $data;
		}
	}
	
	/**
	 * 
	 * 存储session数据
	 * @param string $key
	 * @param string $value
	 */
	public function setUserSession($key, $value) {
		$this->session->set_userdata($key, $value);
	}
	
	/**
	 * 
	 * 获取cookie
	 * @param string $key
	 */
	public function getUserCookie($key) {
		return $this->input->get_cookie($key);
	}
	
	/**
	 * 
	 * 存储cookie
	 * @param string $key
	 * @param string $value
	 * @param string $path
	 * @param string $expire
	 */
	public function setUserCookie($key, $value, $path = '/', $expire) {
		if (!$expire) {
			$expire = time()+60*60*24*30;//30日
		}
		setcookie($key, $value, $expire, $path);
	}
	
	/**
	 * 
	 * 清楚cookie
	 * @param string $key
	 * @param string $path
	 */
	public function clearUserCookie($key, $path = '/') {
		setcookie($key, '', '', $path);
	}
	
	/**
	 * 
	 * 生成URL
	 * @param string $controller
	 * @param string $action
	 * @param string $id
	 */
	public function generateURL($controller, $action, $id) {
		return site_url(array($controller, $action, $id));
	}
	
	/**
	 * 
	 * 获取当前URL
	 */
	public function getCurrentURL() {
		return current_url();
	}
	
	/**
	 * 
	 * 获取当前控制器名
	 */
	public function getControllerName() {
		return $this->uri->rsegment(1);
	}
	
	/**
	 * 
	 * 获取当前方法名
	 */
	public function getActionName() {
		return $this->uri->rsegment(2);
	}
	
	/**
	 * 
	 * 跳转到指定控制器，指定方法
	 * @param string $controller
	 * @param string $action
	 */
	public function redirect($controller, $action) {
		redirect("/$controller/$action/", 'refresh');
	}
	
	/**
	 * 
	 * 跳转到指定URL
	 * @param string $url
	 */
	public function redirectURL($url) {
		redirect($url, 'refresh');
	}
}